/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： SystemController.java
 * Date：18-11-5 上午11:10
 * Author: guomw
 */

package com.guomw.report.web.controller;

import com.guomw.report.common.enums.AppCode;
import com.guomw.report.common.utils.ApiResult;
import com.guomw.report.service.annotations.OperatorId;
import com.guomw.report.service.service.upload.StaticResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author guomw
 * @date 2018/9/17
 */
@Controller
@RequestMapping("sys")
public class SystemController {

    @Autowired
    private StaticResourceService resourceService;

    /**
     * 菜单管理页面
     *
     * @return
     */
    @RequestMapping("/menus")
    public String menusPage() {
        return "system/menus";
    }

    /**
     * 分组管理页面
     *
     * @return
     */
    @RequestMapping("/groups")
    public String groupsPage() {
        return "system/groups";
    }

    /**
     * 角色管理页面
     *
     * @return
     */
    @RequestMapping("/roles")
    public String rolesPage() {
        return "system/roles";
    }

    /**
     * 账户管理页面
     *
     * @return
     */
    @RequestMapping("/manager")
    public String managerPage() {
        return "system/manager";
    }


    /**
     * 上传图片
     *
     * @param file
     * @return
     * @throws IOException
     * @throws URISyntaxException
     */
    @RequestMapping(value = "/ajax/upload", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult uploadImage(@OperatorId Integer operatorId,
                                 @RequestParam(value = "img", required = false) MultipartFile file,
                                 @RequestParam(value = "path", defaultValue = "article") String clientPath
    ) throws IOException, URISyntaxException {
        if(operatorId==null||operatorId<=0){
            return ApiResult.resultWith(AppCode.NOT_AUTHORITY);
        }

        if (StringUtils.isEmpty(clientPath)) {
            clientPath = "none";
        }
        //上传封面图片
        if (file != null) {
            String fileName = file.getOriginalFilename();
            if (!resourceService.typeIsAllow(fileName)) {

                return ApiResult.resultWith(AppCode.PARAMETER_ERROR, "请检查图片格式是否正确");
            }
            String savePath = MessageFormat.format(StaticResourceService.IMG, clientPath, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmSS")))
                    + resourceService.getSuffix(fileName);
            URI uri = resourceService.uploadResource(savePath, file.getInputStream());
            Map<String, String> map = new HashMap<>(1);
            map.put("url", uri.toString());
            return ApiResult.resultWith(AppCode.SUCCESS, map);
        }
        return ApiResult.resultWith(AppCode.ERROR, "请上传图片");
    }

}
